package Thread;

import java.util.concurrent.TimeUnit;

public class notifyAll {
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        new Thread(new Test4Thread1(lock)).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(new Test4Thread2(lock)).start();
        TimeUnit.SECONDS.sleep(2);
        new Thread(new Test4Thread3(lock)).start();
    }

}

class Test4Thread1 implements Runnable {
    private Object lock;

    public Test4Thread1(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        synchronized (lock) {
            System.out.println("线程1等待中...");
            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程1被唤醒...");
        }
    }
}

class Test4Thread2 implements Runnable {
    private Object lock;

    public Test4Thread2(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        synchronized (lock) {
            System.out.println("线程2等待中...");
            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2被唤醒...");
        }
    }
}

class Test4Thread3 implements Runnable {
    private Object lock;

    public Test4Thread3(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        synchronized (lock) {
            System.out.println("线程3开始唤醒...");
            lock.notifyAll();
            System.out.println("线程3唤醒完毕...");
        }
    }
}